package com.jftone.bh.app.action;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.model.NtbMoment;
import com.jftone.bh.app.model.NtbNewslist;
import com.jftone.bh.app.service.MomentService;
import com.jftone.bh.app.service.OSSFileManagerService;
import com.jftone.bh.app.util.UploadFile;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Controller;
import org.jftone.config.Const;
import org.jftone.exception.ActionException;
import org.jftone.util.DataMap;
import org.jftone.util.IData;
import org.jftone.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller(mapping="/momentAction")
public class MomentAction extends BaseAction {

	private Logger logger = Logger.getLogger(MomentAction.class);

	@Autowired
	MomentService service;

	public void query() throws ActionException {
		IData<String, Object> data = getData();
		String userId = data.getString("userId", "");
		String userName = data.getString("userName", "");
        String status = data.getString("status", "");
		String sTime = data.getString("sTime", "");
		String eTime = data.getString("eTime", "");
		this.putRenderInfo("sTime", sTime);
		this.putRenderInfo("userName", userName);
		this.putRenderInfo("eTime", eTime);
		this.putRenderInfo("userId", userId);
        this.putRenderInfo("status", status);
		try {
			//设置功能按钮
			setMenuFuncDerictive();
			//设置分页组件
			setPageDerictive();
			putParam("sTime", sTime);
			putParam("eTime", eTime);
			putParam("status", status);
			putParam("userName", userName);
			putParam("userId", userId);
			putParam(SysConst.QUERY_PARAMS, getParam());

			List<IData<String, Object>> list = service.queryPageList(data, getPage());
			this.putRenderInfo("list", list);
			this.render("moment/moment_list.html");
		} catch (Exception e) {
			logger.error("查询骉圈列表错误" + e.getMessage(), e);
			throw new ActionException("查询骉圈列表错误" + e.getMessage(), e);
		}
	}

	public void add() throws ActionException {
		IData<String, Object> moment = new DataMap<String, Object>();
		this.putRenderInfo("moment",moment);
		this.render("moment/moment_form.html");
	}

	public void edit() throws ActionException {
		IData<String, Object> data = getData();
		String dataId = data.getString("dataId");
		if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
			this.alertMsg("参数传值错误");
			return;
		}
		try {
			IData<String, Object> inData = new DataMap<String, Object>();
			inData.put("id", Integer.parseInt(dataId));
			IData<String, Object> moment = service.getNtbMoment(inData);
			if(moment.isEmpty()){
				this.alertMsg("参数传值错误");
				return;
			}
			this.putRenderInfo("moment", moment);
			this.render("moment/moment_form.html");
		} catch (Exception e) {
			logger.error("编辑骉圈数据错误"+e.getMessage(), e);
			throw new ActionException("编辑骉圈信息错误"+e.getMessage(), e);
		}
	}

	public void save() throws ActionException{
		boolean updateFlag = false;
		IData<String, Object> data = getData();
		String momentId = data.getString("momentId");
		if(!StringUtil.isBlank(momentId) && StringUtil.isNumber(momentId)){
			updateFlag = true;
		}
		try {
			int resCode = service.saveModel(data, updateFlag);
			this.showMsg("骉圈信息"+(updateFlag? "保存" : "新增")+"成功", "?method=query");
		} catch (Exception e) {
			logger.error("保存骉圈信息错误"+e.getMessage(), e);
			throw new ActionException("保存骉圈信息错误"+e.getMessage(), e);
		}
	}

    public void delete() throws ActionException{
        IData<String, Object> data = getData();
        String dataId = data.getString("dataId", "");
        if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
            this.alertMsg("参数传值错误");
            return;
        }
        try {
            int resultCode = service.delete(Long.parseLong(dataId));
            if(resultCode == 1){
                this.alertMsg("数据错误，请刷新后重试");
                return;
            }
            this.showMsg("删除成功", "?method=query");
        } catch (Exception e) {
            logger.error("删除数据错误"+e.getMessage(), e);
            throw new ActionException("删除数据错误"+e.getMessage(), e);
        }

    }


	public void uploadFile() throws ActionException {
		IData<String, Object> data = getData();
		String imgSrc = data.getString("imgSrc", "");
		Long imgId = data.getLong("imgId", 0L);
		try {
			this.putRenderInfo("imgSize", 10 * 1024*1024);
			putRenderInfo("resultCode", data.getString("resultCode", "-1"));
			putRenderInfo("imgSrc", imgSrc);
			putRenderInfo("imgId", imgId);
			putRenderInfo("resUrl", imgId);
			this.render("moment/moment_upload.html");
		} catch (Exception e) {
			logger.error("上传文件页面加载错误" + e.getMessage(), e);
			throw new ActionException("上传文件页面加载错误" + e.getMessage(), e);
		}
	}

	public void upload() throws Exception {
		Long maxSize = 10 * 1024*1024L;
		String redirectUrl ="momentAction.do?method=uploadFile";
		this.upload(StringUtil.getRandomCode(12, StringUtil.CHARNUM_MODE), UploadFile.FileDir.MOMENT, maxSize, 10 * 1024, redirectUrl);
	}
}
